package com.wzh.util.file;

import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;

import java.io.*;
import java.net.URLEncoder;
import java.util.Collections;

/**
 * @Author zh wang
 * @Create 2025/5/26 20:11
 */
@Slf4j
public class FileUtils {
    /**
     * 限制文件在许可的目录内
     * @param fileName
     * @param permitPath
     * @return
     */
    public static String validateFilePath(String fileName, String permitPath) {
        File file = new File(fileName);
        try {
            String canonicalPath = file.getCanonicalPath();
            File permitDir = new File(permitPath);
            String permitDirCanonicalPath = permitDir.getCanonicalPath();
            if (!canonicalPath.startsWith(permitDirCanonicalPath)) {
                throw new RuntimeException("文件不在许可目录内");
            } else {
                return canonicalPath;
            }
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
    public static ResponseEntity downloadResource(byte[] bytes, String resourceName) {
        return downloadResource(new ByteArrayInputStream(bytes), resourceName);
    }

    public static ResponseEntity downloadResource(String resourcePath, String resourceName) {
        try {
            return downloadResource(new FileInputStream(new File(resourcePath)), resourceName);
        } catch (FileNotFoundException e) {
            log.error("file not found, resourcePath: {}", resourcePath, e);
            throw new RuntimeException(e);
        }

    }
    public static ResponseEntity downloadResource(String parentPath, String resourcePath, String resourceName) {
        try {
            String canonicalPath = validateFilePath(resourcePath, parentPath);
            return downloadResource(new FileInputStream(new File(canonicalPath)), resourceName);
        } catch (FileNotFoundException e) {
            log.error("file not found, resourcePath: {}", resourcePath, e);
            throw new RuntimeException(e);
        }

    }

    public static ResponseEntity downloadResource(InputStream inputStream, String resourceName) {
        InputStreamResource resource = new InputStreamResource(inputStream);
        HttpHeaders httpHeaders = new HttpHeaders();
        try {
            httpHeaders.set("Content-Disposition",
                    "attachment; filename=" + URLEncoder.encode(resourceName, "UTF-8"));
        } catch (Exception e) {
            httpHeaders.setContentDispositionFormData("attachment", resourceName);
        }
        httpHeaders.setAccessControlExposeHeaders(Collections.singletonList("Content-Disposition"));

        return ResponseEntity.ok()
                .headers(httpHeaders)
                .contentType(MediaType.parseMediaType("application/" + resourceName.substring(resourceName.lastIndexOf(".") + 1)))
                .body(resource);
    }
}
